<?php
  //==============================================
  // HTTP Server SW
  //============================================== 
  // GET
  // http://IPADDRESS/m2m.php?id=1001&a0=0&a1=1&a2=2&a3=3&a4=4&a5=5
  //==============================================

  //============================
function readCmd($id) 
  //============================
{
  $file = 'ctrl-'.$id.'.arh';
  
  // if (file_exists($file)) 
  //  {
      $in  = fopen($file,"r");
      if($in)
	{
	  while (!feof($in))
	    {
	      $row  = fgets($in);
	      echo($row); // Control Command to Processing Module
	    }
	  fclose($in);
	}
      unlink($file); // Execute command once - delete file
      //    }
}
//============================

// Declaration
//==========================
$analog  = array();
$oneWire = array();

$id     = $_GET['id'];
$ard    = $_GET['ard'];
$ip     = $_GET['ip'];  // Local IP adress of Raspberry Pi
$hb     = $_GET['hb'];  // Heart Beat   millisec
$msg    = $_GET['msg']; // Message from RaspberryPi

$s_day    = date('d');
$s_month  = date('m');
$s_year   = date('Y');
$s_sec    = date('s');
$s_min    = date('i');
$s_hour   = date('H');

if($id > 999 && $id < 10000) // Valid client id
  {
    if($msg==10) // Somebody knocking
      {
	echo("1 $id");
        $file = $id.'.ip'; 
        $out = fopen($file, 'w');
        fwrite($out, $ip); 
        fclose($out); 
        exit;
      }
    if($msg==11) // Heartbeat
      {
	//echo("2 $id");
        // Save timestamp + 10sec
        $time = time();
        $now = date('Y m d H i s');
        $now_plus = date('Y m d H i s',strtotime('+10 seconds',$time));
        //echo ("Now:$now\n");
        //echo ("+10:$now_plus_10s\n");
        $file = $id.'.hb'; 
        $out = fopen($file, 'w');
        fwrite($out, $now_plus); 
        fclose($out); 
        readCmd($id); // check if any command has been initated from user
        exit;
      }

    readCmd($id); // check if any command has been initated from user
      
    $ddrd    = $_GET['ddrd'];
    $ddrb    = $_GET['ddrb'];
    $ddrc    = $_GET['ddrc'];
    $portd   = $_GET['portd'];
    $portb   = $_GET['portb'];
    $portc   = $_GET['portc'];
    $pind    = $_GET['pind'];
    $pinb    = $_GET['pinb'];
    $pinc    = $_GET['pinc'];

    $analog[0]  = $_GET['a0'];
    $analog[1]  = $_GET['a1'];
    $analog[2]  = $_GET['a2'];
    $analog[3]  = $_GET['a3'];
    $analog[4]  = $_GET['a4'];
    $analog[5]  = $_GET['a5'];

    $oneWire[0]  = 0;
    $oneWire[0]  = $_GET['nw'];
    $oneWire[1]  = $_GET['w1'];
    $oneWire[2]  = $_GET['w2'];
    $oneWire[3]  = $_GET['w3'];
    $oneWire[4]  = $_GET['w4'];
    $oneWire[5]  = $_GET['w5'];

    // Log analog values to file (frequency = heartbeat
    $file = 'data-'.$id.'-'.$s_year.'-'.$s_month.'-'.$s_day.".arh"; 
    $out = fopen($file, 'a');
    $data = $s_hour." ".$s_min." ".$s_sec;
    for($ii=0;$ii<=5;$ii++)$data = $data." ".$analog[$ii];
    for($ii=1;$ii<=5;$ii++)$data = $data." ".$oneWire[$ii];
    $data = $data."\n";
    fwrite($out, $data); 
    fclose($out);

    // Save latest status of Arduino board to file    
    $file = 'status-'.$id.".arh"; 
    $out = fopen($file, 'w');

    $data = 'ARDUINO '.$ard."\n";fwrite($out, $data); 
    $data = 'HB    '.$hb."\n";fwrite($out, $data); 
    $data = 'YEAR  '.$s_year."\n";fwrite($out, $data); 
    $data = 'MONTH '.$s_month."\n";fwrite($out, $data); 
    $data = 'DAY   '.$s_day."\n";fwrite($out, $data); 
    $data = 'HOUR  '.$s_hour."\n";fwrite($out, $data); 
    $data = 'MIN   '.$s_min."\n";fwrite($out, $data); 
    $data = 'SEC   '.$s_sec."\n";fwrite($out, $data); 
    
    $data = 'DDRD  '.$ddrd."\n";fwrite($out, $data);
    $data = 'DDRB  '.$ddrb."\n";fwrite($out, $data);
    $data = 'DDRC  '.$ddrc."\n";fwrite($out, $data);
    $data = 'PORTD '.$portd."\n";fwrite($out, $data);
    $data = 'PORTB '.$portb."\n";fwrite($out, $data);
    $data = 'PORTC '.$portc."\n";fwrite($out, $data); 
    $data = 'PIND  '.$pind."\n";fwrite($out, $data); 
    $data = 'PINB  '.$pinb."\n";fwrite($out, $data); 
    $data = 'PINC  '.$pinc."\n";fwrite($out, $data);
     
    $data = 'A0    '.$analog[0]."\n";fwrite($out, $data); 
    $data = 'A1    '.$analog[1]."\n";fwrite($out, $data); 
    $data = 'A2    '.$analog[2]."\n";fwrite($out, $data); 
    $data = 'A3    '.$analog[3]."\n";fwrite($out, $data); 
    $data = 'A4    '.$analog[4]."\n";fwrite($out, $data); 
    $data = 'A5    '.$analog[5]."\n";fwrite($out, $data); 

    if($oneWire[0])
      {
	$data = 'W0    '.$oneWire[0]."\n";fwrite($out, $data); 
	$data = 'W1    '.$oneWire[1]."\n";fwrite($out, $data); 
	$data = 'W2    '.$oneWire[2]."\n";fwrite($out, $data); 
	$data = 'W3    '.$oneWire[3]."\n";fwrite($out, $data); 
	$data = 'W4    '.$oneWire[4]."\n";fwrite($out, $data); 
	$data = 'W5    '.$oneWire[5]."\n";fwrite($out, $data); 
      }

    fclose($out);
    
    // Save latest analog values with time stamp
    $file = 'now-'.$id.".arh"; 
    $out = fopen($file, 'w');
    $data = $s_year."\n";fwrite($out, $data); 
    $data = $s_month."\n";fwrite($out, $data); 
    $data = $s_day."\n";fwrite($out, $data); 
    $data = $s_hour."\n";fwrite($out, $data); 
    $data = $s_min."\n";fwrite($out, $data); 
    $data = $s_sec."\n";fwrite($out, $data); 

    $data = $analog[0]."\n";fwrite($out, $data); 
    $data = $analog[1]."\n";fwrite($out, $data); 
    $data = $analog[2]."\n";fwrite($out, $data); 
    $data = $analog[3]."\n";fwrite($out, $data); 
    $data = $analog[4]."\n";fwrite($out, $data); 
    $data = $analog[5]."\n";fwrite($out, $data); 

    $data = $oneWire[0]."\n";fwrite($out, $data); 
    $data = $oneWire[1]."\n";fwrite($out, $data); 
    $data = $oneWire[2]."\n";fwrite($out, $data); 
    $data = $oneWire[3]."\n";fwrite($out, $data); 
    $data = $oneWire[4]."\n";fwrite($out, $data); 
    $data = $oneWire[5]."\n";fwrite($out, $data); 

    fclose($out); 
  }
?>
